#include "PreView.h"
#include<Util/IconHelper.h>
/**
 * @brief PreView::PreView ,include: button Area、Tab wedget(include mdlList、Method list)、preview area(mdl、method)
 * @param parent
 */
PreView::PreView(QWidget *parent) : QWidget(parent)
{
    this->initUI();
}

void PreView::initUI(){
    QGridLayout *MainLayout = new QGridLayout();
    MainLayout->setSpacing(10);
    MainLayout->setMargin(1);
    this->setLayout(MainLayout);


    //button area
    QHBoxLayout *ButtonArea = new QHBoxLayout();
    ButtonArea->setMargin(0);
    ButtonArea->setSpacing(0);


    QToolButton *OpenMdlButton = new QToolButton();
    OpenMdlButton->setToolTip(tr("Open Mdl"));
    IconHelper::Instance()->SetIcon(OpenMdlButton,QChar(0xf07c),12);
    OpenMdlButton->setObjectName(tr("OpenMdlButton"));



    QToolButton *Settings = new QToolButton();
    Settings->setToolTip(tr("Set Schema"));
    Settings->setObjectName(tr("Settings"));
    IconHelper::Instance()->SetIcon(Settings,QChar(0xf085),12);



    QToolButton *PackButton  = new QToolButton();
    PackButton->setToolTip(tr("Pack"));
    PackButton->setObjectName(tr("Pack"));
    IconHelper::Instance()->SetIcon(PackButton,QChar(0xf187),12);

    QToolButton *MdlViewButton  = new QToolButton();
    MdlViewButton->setToolTip(tr("Mdl View"));
    MdlViewButton->setObjectName(tr("MdlViewButton"));
    IconHelper::Instance()->SetIcon(MdlViewButton,QChar(0xf0e8),12);

    QToolButton *MethodsViewButton  = new QToolButton();
    MethodsViewButton->setToolTip(tr("Methods View"));
    MethodsViewButton->setObjectName(tr("MethodsViewButton"));
    IconHelper::Instance()->SetIcon(MethodsViewButton,QChar(0xf0ae),12);

    ButtonArea->addWidget(OpenMdlButton,1,Qt::AlignLeft);
    ButtonArea->addWidget(MdlViewButton,1,Qt::AlignLeft);
    ButtonArea->addWidget(MethodsViewButton,1,Qt::AlignLeft);
    ButtonArea->addWidget(Settings,1,Qt::AlignLeft);
    ButtonArea->addWidget(PackButton,1,Qt::AlignLeft);


    //preview left

    PreviewLeftListTab = new  QTabWidget();
    PreviewLeftListTab->setTabPosition(QTabWidget::South);
    PreviewLeftListTab->setTabsClosable(true);


    //List
    this->MdlListWidget = new QListWidget();
    this->MethodsListWidget = new QTreeWidget();


    this->MdlListWidget->setContextMenuPolicy(Qt::CustomContextMenu);

    this->addLeftTabWidget(PreviewLeftListTab,"MDLs","MDLs","MDLsLabel",this->MdlListWidget);

    this->ViewScrollArea = new QScrollArea();
    this->scene = new mScene();
    this->scene->setSceneRect(QRectF(0, 0, 500, 500));
    ViewArea = new QGridLayout();
    ViewArea->setMargin(0);
    ViewScrollArea->setLayout(ViewArea);
    this->ViewArea->addWidget(new QGraphicsView(scene));


    //preview right
    mMethodView = new MethodsView();
    PreviewRight = new QTabWidget();
    PreviewRight->setTabsClosable(true);
    PreviewRight->setTabPosition(QTabWidget::North);
    PreviewRight->addTab(this->ViewScrollArea,tr("Mdl"));


    MainLayout->addLayout(ButtonArea,0,0,1,3);

    MainLayout->addWidget(PreviewLeftListTab,1,0,10,4);
    MainLayout->addWidget(PreviewRight,1,4,10,28);


    //pack
    pUI = new PackagerUI();


    //State info
    mStateInfoView = new StateInfoView();

    connect(this->scene,SIGNAL(on_ModelState_doubleClicked(CState*)),this,SLOT(showStateInfo(CState*)));
    connect(PackButton,SIGNAL(clicked(bool)),this,SLOT(on_toPack_clicked()));
    connect(this->MdlListWidget,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(on_customContextMenuRequested_(QPoint)));
    connect(Settings,SIGNAL(clicked(bool)),this,SLOT(on_settings_clicked()));
    connect(OpenMdlButton,SIGNAL(clicked(bool)),this,SLOT(on_add_mdl_clicked()));
    connect(MdlViewButton,SIGNAL(clicked(bool)),this,SIGNAL(mdlView_clicked()));
    connect(MethodsViewButton,SIGNAL(clicked(bool)),this,SIGNAL(methodsView_clicked()));
    connect(PreviewLeftListTab,SIGNAL(tabBarClicked(int)),this,SIGNAL(LeftTabClicked(int)));
    connect(PreviewRight,SIGNAL(tabCloseRequested(int)),this,SLOT(on_PreviewRight_close_tab_clicked(int)));
    connect(PreviewLeftListTab,SIGNAL(tabCloseRequested(int)),this,SLOT(on_PreviewLeftListTab_close_tab_clicked(int)));
    connect(this->MethodsListWidget,SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(add_MethodView()));
    connect(this->MethodsListWidget,SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(on_methodsList_item_clicked(QTreeWidgetItem*,int)));


}

void PreView::showStateInfo(CState *StateInfo){
    mStateInfoView->inputData(StateInfo);
    mStateInfoView->show();
}

void PreView::on_customContextMenuRequested_(QPoint pos){


    QAction *addItem = new QAction(tr("Add"), this);
    QAction *deleteItem = new QAction(tr("Delete"), this);
    QAction *clearItem = new QAction(tr("Clear"), this);
    QMenu *popMenu = new QMenu( this );
    popMenu->addAction(addItem);
    popMenu->addAction( deleteItem );
    popMenu->addAction( clearItem );

    QListWidgetItem* curItem = this->MdlListWidget->itemAt( pos );
        if( curItem == NULL ){
          deleteItem->setEnabled(false);
        }else{
          deleteItem->setEnabled(true);

        }
        connect( addItem, SIGNAL(triggered() ), this, SLOT( on_addItem_clicked()) );
        connect( deleteItem, SIGNAL(triggered() ), this, SLOT( on_deleteItem_clicked()) );
        connect( clearItem, SIGNAL(triggered() ), this, SLOT( on_clearItem_clicked()) );


        popMenu->exec( QCursor::pos() );
        delete popMenu;
        delete addItem;
        delete deleteItem;
        delete clearItem;
}

void PreView::on_addItem_clicked(){

    emit addItem_clicked();
}
void PreView::on_deleteItem_clicked(){
     emit deleteItem_clicked();
}
void PreView::on_clearItem_clicked(){
     emit clearItem_clicked();
}

void PreView::on_toPack_clicked(){
    emit toPack_clicked();
}

void PreView::on_settings_clicked(){
    emit settings_clicked();
}

void PreView::on_add_mdl_clicked(){
    this->add_mdl_clicked();
}

void PreView::on_PreviewRight_close_tab_clicked(int index){
    PreviewRight->removeTab(index);
}
void PreView::on_PreviewLeftListTab_close_tab_clicked(int index){
    PreviewLeftListTab->removeTab(index);
}

void PreView::add_MdlListTab(){
    for(int i=0;i<this->PreviewLeftListTab->count();i++){
        if(this->PreviewLeftListTab->tabText(i)=="MDLs"){
            this->PreviewLeftListTab->setCurrentIndex(i);
            return ;
        }
    }

    int mIndex = this->addLeftTabWidget(PreviewLeftListTab,"MDLs","MDLs","MDLsLabel",this->MdlListWidget);

    this->PreviewLeftListTab->setCurrentIndex(mIndex);
}

void PreView::add_MethodListTab(){
    for(int i=0;i<this->PreviewLeftListTab->count();i++){
        if(this->PreviewLeftListTab->tabText(i)=="Methods"){
            this->PreviewLeftListTab->setCurrentIndex(i);
            this->refreshMethodList();
            return ;
        }
    }

    int mIndex =this->addLeftTabWidget(this->PreviewLeftListTab,"Methods","Methods","MethodsLabel",this->MethodsListWidget);
    this->PreviewLeftListTab->setCurrentIndex(mIndex);
    this->refreshMethodList();




}

int PreView::addLeftTabWidget(QTabWidget *ParentTabWidget,QString LabelName,QString TabText,QString ObjectName,QWidget *AddedWidget){
    QVBoxLayout *MDLsLayout=new QVBoxLayout();
    MDLsLayout->setMargin(0);
    MDLsLayout->setSpacing(0);
    int index = ParentTabWidget->addTab(new QWidget(),TabText);
    PreviewLeftListTab->widget(index)->setLayout(MDLsLayout);
    QLabel *MDLsLabel = new QLabel(LabelName);
    MDLsLabel->setObjectName(ObjectName);
    MDLsLayout->addWidget(MDLsLabel,1);
    MDLsLayout->addWidget(AddedWidget,20);
    return index;
}

void PreView::refreshMethodList(){
    this->MethodsListWidget->clear();
    //Add items
    mConversionMethods = new QList<CMethodsInfo *>();
    QDir  ProjectDir(QApplication::applicationDirPath());
    mDirPath = ProjectDir.absolutePath();
    QString DirPath=mDirPath+"/config/project.xml";
    if(!get_setInfo::readMethods(mDirPath,DirPath,mConversionMethods)){
        return ;
    }

    this->MethodsListWidget->setColumnCount(mConversionMethods->size());
    this->MethodsListWidget->setHeaderHidden(true);

    for(int i=0;i<mConversionMethods->size();i++){
        CMethodsInfo *mMethods = mConversionMethods->at(i);

         QTreeWidgetItem *MethodsFoldItem = new QTreeWidgetItem();
         MethodsFoldItem->setText(0,mMethods->getPath());
         QList<QTreeWidgetItem *> *MethodsItemList = new QList<QTreeWidgetItem *>();

         for(int j=0;j<mMethods->getCount();j++){
             MethodsItemList->append(new QTreeWidgetItem(QStringList(QFileInfo(mMethods->getMethod(j)).fileName())));
         }
         MethodsFoldItem->addChildren(*MethodsItemList);
         this->MethodsListWidget->insertTopLevelItem(0,MethodsFoldItem);
    }
}

void PreView::add_MdlView(){
    for(int i=0;i<this->PreviewRight->count();i++){
        if(this->PreviewRight->tabText(i)=="Mdl"){
            this->PreviewRight->setCurrentIndex(i);
            return ;
        }
    }
     int mIndex = PreviewRight->addTab(this->ViewScrollArea,tr("Mdl"));
      this->PreviewRight->setCurrentIndex(mIndex);
}

void PreView::add_MethodView(){
    for(int i=0;i<this->PreviewRight->count();i++){
        if(this->PreviewRight->tabText(i)=="Method"){
            this->PreviewRight->setCurrentIndex(i);
            return ;
        }
    }
    int mIndex = PreviewRight->addTab(mMethodView,tr("Method"));
    PreviewRight->setCurrentIndex(mIndex);
}

void PreView::add_PackView(){
    for(int i=0;i<this->PreviewRight->count();i++){
        if(this->PreviewRight->tabText(i)=="Pack"){
            this->PreviewRight->setCurrentIndex(i);
            return ;
        }
    }
    int mIndex = PreviewRight->addTab(pUI,tr("Pack"));
    PreviewRight->setCurrentIndex(mIndex);
}

void PreView::on_methodsList_item_clicked(QTreeWidgetItem *item, int ColumnIndex){
    for(int i=0;i<this->mConversionMethods->count();i++){
        CMethodsInfo *mMethods = mConversionMethods->at(i);
        for(int j=0;j<mMethods->getCount();j++){
            if(QFileInfo(mMethods->getMethod(j)).fileName()==item->text(ColumnIndex)){
                QString line="no schema";
                QFile file(mMethods->getMethod(j)+"\\"+QFileInfo(mMethods->getMethod(j)).fileName()+".xml");
                     if (file.open(QIODevice::ReadOnly | QIODevice::Text)){
                        line="";
                         QTextStream in(&file);
                         while (!in.atEnd()) {
                             line += in.readLine();
                             line +="\n";
                         }
                     }
                  this->mMethodView->ShowInfo(item->text(ColumnIndex),mMethods->getMethod(j),"",line);
            }
        }
    }
}



